package com.sgkusbcamdemo;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.gkusbsdk.SZUSBCommand;
import com.gkusbsdk.SZUSBFireware;
import com.gkusbsdk.SZUSBNormal;
import com.gkusbsdk.SZUSBRecordResolution;
import com.gkusbsdk.SZUSBRecordTime;
import com.gkusbsdk.SZUSBSensor;
import com.gkusbsdk.SZUSBSwitch;
import com.qitianyong.selfclass.BUS_MESSAGE;
import com.qitianyong.selfclass.CONSTANT;
import com.qitianyong.selfclass.GKDevice;
import com.qitianyong.selfclass.Message2EventBus;

public class SettingActivity extends BaseActivity implements View.OnClickListener {

    private Button getResolution;
    private Button setResolution;
    private Button getRecordTime;
    private Button setRecordTime;
    private Button getSensor;
    private Button setSensor;
    private Button getAutoRecord;
    private Button setAutoRecord;
    private Button getAudio;
    private Button setAudio;
    private Button getVersion;

    private Button setSwitchCamera;
    private Button getSwitchCamera;
    private DialogInterface.OnClickListener setResolutionOnClickListener;
    private DialogInterface.OnClickListener setRecordOnClickListener;
    private DialogInterface.OnClickListener setSensorOnClickListener;
    private DialogInterface.OnClickListener setAutoRecordOnClickListener;
    private DialogInterface.OnClickListener setAudioOnClickListener;

    private DialogInterface.OnClickListener setSwitchOnClickListener;

    /*
        如有需求进入界面后展示所有设置选项的状态，可一起发送所有的获取选项
            GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RESOLUTION, null);
            GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORD_TIME, null);
            GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_SENSOR, null);
            GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORD_AUTO_MANAL, null);
            GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORD_AUDIO, null);
            GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORDER_VERSION, null);
    */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        //8602a才可设置分辨率，获取下设备信息、然后设置界面
        GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORDER_VERSION, null);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return false;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void initView() {
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        getResolution = (Button) findViewById(R.id.get_resolution);
        setResolution = (Button) findViewById(R.id.set_resolution);
        getRecordTime = (Button) findViewById(R.id.get_record_time);
        setRecordTime = (Button) findViewById(R.id.set_record_time);
        getSensor = (Button) findViewById(R.id.get_sensor);
        setSensor = (Button) findViewById(R.id.set_sensor);
        getAutoRecord = (Button) findViewById(R.id.get_auto_record);
        setAutoRecord = (Button) findViewById(R.id.set_auto_record);
        getAudio = (Button) findViewById(R.id.get_audio);
        setAudio = (Button) findViewById(R.id.set_audio);
        getVersion = (Button) findViewById(R.id.get_version);

        setSwitchCamera = (Button) findViewById(R.id.set_switch_camera);
        getSwitchCamera = (Button) findViewById(R.id.get_switch_camera);

        setSwitchCamera.setOnClickListener(this);
        setSwitchCamera.setOnClickListener(this);
        getResolution.setOnClickListener(this);
        setResolution.setOnClickListener(this);
        getRecordTime.setOnClickListener(this);
        setRecordTime.setOnClickListener(this);
        getSensor.setOnClickListener(this);
        setSensor.setOnClickListener(this);
        getAutoRecord.setOnClickListener(this);
        setAutoRecord.setOnClickListener(this);
        getAudio.setOnClickListener(this);
        setAudio.setOnClickListener(this);
        getVersion.setOnClickListener(this);
        setResolutionOnClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SZUSBRecordResolution resolution = new SZUSBRecordResolution();
                resolution.__resolution = which;
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_RESOLUTION,
                        resolution);
                showDialog();
                dialog.dismiss();
            }
        };

        setSwitchOnClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SZUSBSwitch szusbSwitch = new SZUSBSwitch();
                szusbSwitch.__type = which;
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_CAMERA_SWITCH,
                        szusbSwitch);
                showDialog();
                dialog.dismiss();
            }
        };

        setRecordOnClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SZUSBRecordTime recordtime = new SZUSBRecordTime();
                recordtime.__record_time = which;
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_RECORD_TIME,
                        recordtime);
                showDialog();
                dialog.dismiss();
            }
        };
        setSensorOnClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SZUSBSensor sensor = new SZUSBSensor();
                sensor.__lever = which;
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_SENSOR,
                        sensor);
                showDialog();
                dialog.dismiss();
            }
        };
        setAutoRecordOnClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SZUSBNormal normal = new SZUSBNormal();
                if (which == SZUSBCommand.SWITCH.OPEN) {// 发送选中
                    normal.__normal = SZUSBCommand.SWITCH.OPEN;
                } else {
                    normal.__normal = SZUSBCommand.SWITCH.CLOSE;
                }

                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_RECORD_AUTO_MANAL, normal);
                showDialog();
                dialog.dismiss();
            }
        };
        setAudioOnClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SZUSBNormal normal = new SZUSBNormal();
                if (which == SZUSBCommand.SWITCH.OPEN) {// 发送选中
                    normal.__normal = SZUSBCommand.SWITCH.OPEN;
                } else {
                    normal.__normal = SZUSBCommand.SWITCH.CLOSE;
                }

                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_SET_RECORD_AUDIO, normal);
                showDialog();
                dialog.dismiss();

            }
        };
    }

    private void showSetOptionDialog(String[] options, DialogInterface.OnClickListener onClickListener) {
        new AlertDialog
                .Builder(this)
                .setSingleChoiceItems(options, -1, onClickListener)
                .show();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_switch_camera:
                showSetOptionDialog(getResources().getStringArray(R.array.camera_switch_array), setSwitchOnClickListener);
                break;
            case R.id.set_resolution:
                showSetOptionDialog(getResources().getStringArray(R.array.resolution_array), setResolutionOnClickListener);
                break;
            case R.id.set_record_time:
                showSetOptionDialog(getResources().getStringArray(R.array.record_time_array), setRecordOnClickListener);
                break;
            case R.id.set_sensor:
                showSetOptionDialog(getResources().getStringArray(R.array.sensor_array), setSensorOnClickListener);
                break;
            case R.id.set_auto_record:
                showSetOptionDialog(getResources().getStringArray(R.array.on_off_array), setAutoRecordOnClickListener);
                break;
            case R.id.set_audio:
                showSetOptionDialog(getResources().getStringArray(R.array.on_off_array), setAudioOnClickListener);
                break;
            default:
                showDialog();
                break;
        }
        switch (v.getId()) {
            case R.id.get_resolution:
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RESOLUTION, null);
                break;
            case R.id.get_record_time:
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORD_TIME, null);
                break;
            case R.id.get_sensor:
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_SENSOR, null);
                break;
            case R.id.get_auto_record:
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORD_AUTO_MANAL, null);
                break;
            case R.id.get_audio:
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORD_AUDIO, null);
                break;
            case R.id.get_version:
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_RECORDER_VERSION, null);
                break;

            case R.id.get_switch_camera:
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_GET_CAMERA_SWITCH, null);
                break;
        }
    }


    public void onEventMainThread(Message2EventBus message) {
        boolean isHandle = true;
        switch (message.__what) {
            case BUS_MESSAGE.BUS_GET_CAMERA_SWITCH: {
                int result = (int) message.object;
                toast("status:" + result);
                break;
            }
            case BUS_MESSAGE.BUS_GET_RESOLUTION: {

                int result = (int) message.object;
                if (result == SZUSBCommand.RESULT_RESOLUTION.P720) {
                    toast("720P");
                } else if (result == SZUSBCommand.RESULT_RESOLUTION.P1080) {
                    toast("1080P");
                } else if (result == SZUSBCommand.RESULT_RESOLUTION.VGA) {
                    toast("VGA");
                } else {
                    toast("获取失败");
                }

                cancelDialog();
                break;
            }
            case BUS_MESSAGE.BUS_GET_RECORD_TIME: {
                int result = (int) message.object;
                if (result == SZUSBCommand.RESULT_RECORDTIME.ONE_MIN) {
                    toast("1分钟");
                } else if (result == SZUSBCommand.RESULT_RECORDTIME.THREE_MIN) {
                    toast("3分钟");
                } else if (result == SZUSBCommand.RESULT_RECORDTIME.FIVE_MIN) {
                    toast("5分钟");
                } else {
                    toast("获取失败");
                }
                cancelDialog();
                break;
            }
            case BUS_MESSAGE.BUS_GET_SENSOR: {
                int result = (int) message.object;
                if (result == SZUSBCommand.RESULT_LEVER.HIGH) {
                    toast("高");
                } else if (result == SZUSBCommand.RESULT_LEVER.MIDDLE) {
                    toast("中");
                } else if (result == SZUSBCommand.RESULT_LEVER.LOW) {
                    toast("低");
                } else {
                    toast("获取失败");
                }
                cancelDialog();
                break;
            }
            case BUS_MESSAGE.BUS_GET_RECORD_AUTO_MANAL: {
                int result = (int) message.object;
                if (result == SZUSBCommand.SWITCH.OPEN) {
                    toast("自动录像：开");
                } else {
                    toast("自动录像：关");
                }
                cancelDialog();
                break;
            }
            case BUS_MESSAGE.BUS_GET_RECORD_AUDIO: {
                int result = (int) message.object;
                if (result == SZUSBCommand.SWITCH.OPEN) {
                    toast("录音：开");
                } else {
                    toast("录音：关");
                }
                cancelDialog();
                break;
            }
            case BUS_MESSAGE.BUS_GET_RECORDER_VERSION_ERR: {
                toast("获取版本失败");
                cancelDialog();
                break;
            }
            case BUS_MESSAGE.BUS_GET_RECORDER_VERSION_OK: {

                SZUSBFireware fireware = (SZUSBFireware) message.object;
                if (fireware != null) {
                    toast(new String(fireware.__version));
                    if (fireware.__product_type == SZUSBCommand.PRODUCT_TYPE.GK8602_A) {
                        setResolution.setEnabled(true);
                    }
                }
                cancelDialog();
                break;
            }
            case BUS_MESSAGE.BUS_SET_CAMERA_SWITCH:
            case BUS_MESSAGE.BUS_SET_RECORD_AUTO_MANAL:
            case CONSTANT.COMMAND.CMD_SET_RECORD_AUDIO:
            case BUS_MESSAGE.BUS_SET_RESOLUTION:
            case BUS_MESSAGE.BUS_SET_RECORD_TIME:
            case BUS_MESSAGE.BUS_SET_SENSOR: {
                int result = (int) message.object;
                if (result == -1) {
                    toast("设置失败");
                } else {
                    toast("设置成功");
                }
                cancelDialog();
                break;
            }
            case BUS_MESSAGE.BUS_PRINT: {
                cancelDialog();
                break;
            }
            default:
                break;
        }
    }

}
